package com.globalwave.pos.inventory;

import java.util.List;

import junit.framework.Assert;

import org.junit.Test;

import com.globalwave.common.exception.BusinessException;
import com.globalwave.pos.inventory.entity.InventoryBalance;
import com.globalwave.pos.inventory.entity.InventoryTransaction;
import com.globalwave.pos.inventory.service.InventoryBalanceBO;

public class InventoryOnHandBalanceBOTest extends InventoryBaseTestCase {
	@Test
    public void getSalableQuantity_Case1() throws Exception {
		InventoryBalanceBO bo = getInventoryBalanceBO(warehouseCode) ;
		Assert.assertEquals(new Integer(100), bo.getSalableQuantity(stockItemNum1)) ;
    }
	

	@Test
    public void getSalableQuantity_Case2() throws Exception {
		InventoryBalanceBO bo = getInventoryBalanceBO(warehouseCode) ;
		Assert.assertEquals(new Integer(95), bo.getSalableQuantity(stockItemNum1)) ;
    }

	@Test
    public void getSalableQuantity_Case3() throws Exception {
		InventoryBalanceBO bo = getInventoryBalanceBO(warehouseCode) ;
		Assert.assertEquals(new Integer(95), bo.getSalableQuantity(stockItemNum1)) ;
    }

	@Test
    public void getSalableQuantity_Case4() throws Exception {
		InventoryBalanceBO bo = getInventoryBalanceBO(warehouseCode) ;
		Assert.assertEquals(new Integer(95), bo.getSalableQuantity(stockItemNum1)) ;
    }

	@Test
    public void getSalableQuantity_Case5() throws Exception {
		InventoryBalanceBO bo = getInventoryBalanceBO(warehouseCode) ;
		Assert.assertEquals(new Integer(90), bo.getSalableQuantity(stockItemNum1)) ;
    }

	@Test
    public void getSalableQuantity_Case6() throws Exception {
		InventoryBalanceBO bo = getInventoryBalanceBO(warehouseCode) ;
		Assert.assertEquals(new Integer(90), bo.getSalableQuantity(stockItemNum1)) ;
    }
	
	@Test
	public void getSalableQuantity_Case7() throws Exception {
		InventoryBalanceBO bo = getInventoryBalanceBO(warehouseCode) ;
		Assert.assertEquals(new Integer(85), bo.getSalableQuantity(stockItemNum1)) ;
	}
	
	@Test
	public void getSalableQuantity_Case8() throws Exception {
		InventoryBalanceBO bo = getInventoryBalanceBO(warehouseCode) ;
		Assert.assertEquals(new Integer(83), bo.getSalableQuantity(stockItemNum1)) ;
	}
	
	@Test
	public void getSalableQuantity_Case9() throws Exception {
		InventoryBalanceBO bo = getInventoryBalanceBO(warehouseCode) ;
		Assert.assertEquals(new Integer(83), bo.getSalableQuantity(stockItemNum1)) ;
	}

	@Test
	public void validateSalable_Case2() throws Exception {
		String sqlFile = getSqlFileDir() + this.getClass().getSimpleName() + "_getSalableQuantity_Case2.sql" ;
		execute(sqlFile) ;

		InventoryBalanceBO bo = getInventoryBalanceBO(warehouseCode) ;
		try {
		    bo.validateSalable(stockItemNum1, new Integer(94), null) ;
		} catch (BusinessException e) {
			e.printStackTrace() ;
			Assert.assertTrue("Pos5gBizException should not be throwed!", false) ;
		}
		
		try {
		    bo.validateSalable(stockItemNum1, new Integer(95), null) ;
		} catch (BusinessException e) {
			e.printStackTrace() ;
			Assert.assertTrue("Pos5gBizException should not be throwed!", false) ;
		}
		
		
		try {
		    bo.validateSalable(stockItemNum1, new Integer(96), null) ;
			Assert.assertTrue("Over salable, Pos5gBizException should be throwed!", false) ;
		} catch (BusinessException e) {
			e.printStackTrace() ;
			Assert.assertEquals("err.inv.more.than.salable", e.getMessageKey()) ;
		}
	}

	@Test
    public void archiveCurrentPeriod_Case1() throws Exception {
		execute(getSqlFile(this.getClass(), "period")) ;
		InventoryBalanceBO bo = getInventoryBalanceBO(warehouseCode) ;
		Assert.assertEquals("init Available, " , new Integer(90), bo.getSalableQuantity(stockItemNum1)) ;
		
		bo.archiveCurrentPeriod() ;
		bo.clear() ;
		
		Assert.assertEquals("after archiveCurrentPeriod, " , new Integer(90), bo.getSalableQuantity(stockItemNum1)) ;
		
		InventoryBalance balance = getBalance(this.warehouseCode, this.stockItemNum1) ;

		Assert.assertEquals("result - balance.OnHandBalance, " , new Integer(100), balance.getOn_hand_quantity()) ;
		Assert.assertEquals("result - balance.MortgagedQuantity, " , new Integer(10), balance.getMortgaged_quantity()) ;
		

		InventoryTransaction trans = getTempTransaction(this.warehouseCode, this.stockItemNum1) ;

		Assert.assertNull("result - trans must be NULL, " , trans) ;
		
    }
	

	@Test
    public void archiveCurrentPeriod_Case2() throws Exception {
		execute(getSqlFile(this.getClass(), "period")) ;
		InventoryBalanceBO bo = getInventoryBalanceBO(warehouseCode) ;
		Assert.assertEquals("init Available, " , new Integer(95), bo.getSalableQuantity(stockItemNum1)) ;
		
		bo.archiveCurrentPeriod() ;
		bo.clear() ;
		
		Assert.assertEquals("after archiveCurrentPeriod, " , new Integer(95), bo.getSalableQuantity(stockItemNum1)) ;
		
		InventoryBalance balance = getBalance(this.warehouseCode, this.stockItemNum1) ;

		Assert.assertEquals("result - balance.OnHandBalance, " , new Integer(95), balance.getOn_hand_quantity()) ;
		Assert.assertEquals("result - balance.MortgagedQuantity, " , new Integer(0), balance.getMortgaged_quantity()) ;
		

		InventoryTransaction trans = getTempTransaction(this.warehouseCode, this.stockItemNum1) ;

		Assert.assertNull("result - trans must be NULL, " , trans) ;
		
    }
	


	@Test
    public void archiveCurrentPeriod_Case3() throws Exception {
		execute(getSqlFile(this.getClass(), "period")) ;
		InventoryBalanceBO bo = getInventoryBalanceBO(warehouseCode) ;
		Assert.assertEquals("init Available 12345, " , new Integer(90), bo.getSalableQuantity(stockItemNum1)) ;
		Assert.assertEquals("init Available 23456, " , new Integer(94), bo.getSalableQuantity(stockItemNum2)) ;
		
		bo.archiveCurrentPeriod() ;
		bo.clear() ;
		
		Assert.assertEquals("after archiveCurrentPeriod 12345, " , new Integer(90), bo.getSalableQuantity(stockItemNum1)) ;
		Assert.assertEquals("after archiveCurrentPeriod 23456, " , new Integer(94), bo.getSalableQuantity(stockItemNum2)) ;
		
		InventoryBalance balance = getBalance(this.warehouseCode, this.stockItemNum1) ;

		Assert.assertEquals("result - balance.OnHandBalance, " , new Integer(100), balance.getOn_hand_quantity()) ;
		Assert.assertEquals("result - balance.MortgagedQuantity, " , new Integer(10), balance.getMortgaged_quantity()) ;


		balance = getBalance(this.warehouseCode, this.stockItemNum2) ;

		Assert.assertEquals("result - balance.OnHandBalance, " , new Integer(200), balance.getOn_hand_quantity()) ;
		Assert.assertEquals("result - balance.MortgagedQuantity, " , new Integer(100), balance.getMortgaged_quantity()) ;
		

		InventoryTransaction trans = getTempTransaction(this.warehouseCode, this.stockItemNum1) ;

		Assert.assertNull("result - trans must be NULL, " , trans) ;
		
		trans = getTempTransaction(this.warehouseCode, this.stockItemNum2) ;

		Assert.assertNotNull("result - trans must be NULL, " , trans) ;
		Assert.assertEquals("quantity not be changed!" , new Integer(6), trans.getMortgaged_quantity()) ;
		
    }
	
	@Test
	public void archiveCurrentPeriod_Case4() throws Exception {
		execute(getSqlFile(this.getClass(), "period")) ;
		InventoryBalanceBO bo = getInventoryBalanceBO(warehouseCode) ;
		Assert.assertEquals("init Available 12345, " , new Integer(290), bo.getSalableQuantity(stockItemNum1)) ;
		Assert.assertEquals("init Available 23456, " , new Integer(100), bo.getSalableQuantity(stockItemNum2)) ;
		
		bo.archiveCurrentPeriod() ;
		bo.clear() ;
		
		Assert.assertEquals("after archiveCurrentPeriod 12345, " , new Integer(290), bo.getSalableQuantity(stockItemNum1)) ;
		Assert.assertEquals("after archiveCurrentPeriod 23456, " , new Integer(100), bo.getSalableQuantity(stockItemNum2)) ;
		
		InventoryBalance balance = getBalance(this.warehouseCode, this.stockItemNum1) ;
		
		Assert.assertEquals("result - balance.OnHandBalance, " , new Integer(195), balance.getOn_hand_quantity()) ;
		Assert.assertEquals("result - balance.MortgagedQuantity, " , new Integer(5), balance.getMortgaged_quantity()) ;
		
		
		balance = getBalance(this.warehouseCode, this.stockItemNum2) ;
		
		Assert.assertEquals("result - balance.OnHandBalance, " , new Integer(200), balance.getOn_hand_quantity()) ;
		Assert.assertEquals("result - balance.MortgagedQuantity, " , new Integer(100), balance.getMortgaged_quantity()) ;
		
		
		List<Object> transList = queryTempTransaction(this.warehouseCode, this.stockItemNum1) ;
		
		Assert.assertEquals("result - trans must be NULL, " , 1, transList.size()) ;
		
		InventoryTransaction trans = (InventoryTransaction)transList.get(0) ;
		
		Assert.assertEquals("code not be changed!" , "I", trans.getTransaction_code()) ;
		Assert.assertEquals("quantity not be changed!" , new Integer(100), trans.getOn_hand_quantity()) ;
		//Assert.assertEquals("date not be changed!" , new Integer(100), trans.getTransactionDateTime()) ;
		
	}
	

	@Test
    public void archiveCurrentPeriod_Case5() throws Exception {
		execute(getSqlFile(this.getClass(), "period")) ;
		InventoryBalanceBO bo = getInventoryBalanceBO(warehouseCode) ;
		Assert.assertEquals("init Available 12345, " , new Integer(995), bo.getSalableQuantity(stockItemNum1)) ;
		
		bo.archiveCurrentPeriod() ;
		bo.clear() ;
		
		Assert.assertEquals("after archiveCurrentPeriod 12345, " , new Integer(995), bo.getSalableQuantity(stockItemNum1)) ;
		
		InventoryBalance balance = getBalance(this.warehouseCode, this.stockItemNum1) ;

		Assert.assertEquals("result - balance.OnHandBalance, " , new Integer(11000), balance.getOn_hand_quantity()) ;
		Assert.assertEquals("result - balance.MortgagedQuantity, " , new Integer(10005), balance.getMortgaged_quantity()) ;

		

		List<Object> transList = queryTempTransaction(this.warehouseCode, this.stockItemNum1) ;

		Assert.assertEquals("result - trans must be NULL, " , 0, transList.size()) ;
		
		transList = queryTempTransaction(this.warehouseCode, this.stockItemNum2) ;

		Assert.assertEquals("result - trans must not be NULL, " , 1, transList.size()) ;
		
		InventoryTransaction trans = (InventoryTransaction)transList.get(0) ;

		Assert.assertEquals("code not be changed!" , "M", trans.getTransaction_code()) ;
		Assert.assertEquals("quantity not be changed!" , new Integer(6), trans.getMortgaged_quantity()) ;
		//Assert.assertEquals("date not be changed!" , new Integer(100), trans.getTransactionDateTime()) ;
		
    }

	

	@Test
    public void archiveCurrentPeriod_Case6() throws Exception {
		execute(getSqlFile(this.getClass(), "period")) ;
		InventoryBalanceBO bo = getInventoryBalanceBO(warehouseCode) ;
		Assert.assertEquals("init Available 12345, " , new Integer(-1005), bo.getSalableQuantity(stockItemNum1)) ;
		Assert.assertEquals("init Available 23456, " , new Integer(100),   bo.getSalableQuantity(stockItemNum2)) ;
		
		bo.archiveCurrentPeriod() ;
		bo.clear() ;
		
		Assert.assertEquals("after archiveCurrentPeriod 12345, " , new Integer(-1005), bo.getSalableQuantity(stockItemNum1)) ;
		Assert.assertEquals("after archiveCurrentPeriod 23456, " , new Integer(100),   bo.getSalableQuantity(stockItemNum2)) ;
		
		InventoryBalance balance = getBalance(this.warehouseCode, this.stockItemNum1) ;

		Assert.assertEquals("result - balance.OnHandBalance, " , new Integer(0), balance.getOn_hand_quantity()) ;
		Assert.assertEquals("result - balance.MortgagedQuantity, " , new Integer(1005), balance.getMortgaged_quantity()) ;

		 balance = getBalance(this.warehouseCode, this.stockItemNum2) ;

		Assert.assertEquals("result - balance.OnHandBalance, " , new Integer(200), balance.getOn_hand_quantity()) ;
		Assert.assertEquals("result - balance.MortgagedQuantity, " , new Integer(100), balance.getMortgaged_quantity()) ;

		List<Object> transList = queryTempTransaction(this.warehouseCode, this.stockItemNum1) ;

		Assert.assertEquals("result - trans must be NULL, " , 0, transList.size()) ;
		
		transList = queryTempTransaction(this.warehouseCode, this.stockItemNum2) ;

		Assert.assertEquals("result - trans must not be NULL, " , 0, transList.size()) ;
		
    }


	@Test
    public void archiveCurrentPeriod_Case7() throws Exception {
		execute(getSqlFile(this.getClass(), "period")) ;
		InventoryBalanceBO bo = getInventoryBalanceBO(warehouseCode) ;
		Assert.assertEquals("init Available, " , new Integer(95), bo.getSalableQuantity(stockItemNum1)) ;
		Assert.assertEquals("init Available, " , new Integer(-5), bo.getSalableQuantity(stockItemNum2)) ;

		Assert.assertNull("init 23456 not exist, " , getBalance(warehouseCode, stockItemNum2)) ;
		
		bo.archiveCurrentPeriod() ;
		bo.clear() ;
		
		Assert.assertEquals("after archiveCurrentPeriod, " , new Integer(95), bo.getSalableQuantity(stockItemNum1)) ;
		Assert.assertEquals("after archiveCurrentPeriod, " , new Integer(-5), bo.getSalableQuantity(stockItemNum2)) ;
		
		InventoryBalance balance = getBalance(this.warehouseCode, this.stockItemNum1) ;

		Assert.assertEquals("result - balance.OnHandBalance, " , new Integer(100), balance.getOn_hand_quantity()) ;
		Assert.assertEquals("result - balance.MortgagedQuantity, " , new Integer(5), balance.getMortgaged_quantity()) ;
		
		balance = getBalance(this.warehouseCode, this.stockItemNum2) ;

		Assert.assertEquals("result - balance.OnHandBalance, " , new Integer(0), balance.getOn_hand_quantity()) ;
		Assert.assertEquals("result - balance.MortgagedQuantity, " , new Integer(5), balance.getMortgaged_quantity()) ;
		
		InventoryTransaction trans = getTempTransaction(this.warehouseCode, this.stockItemNum2) ;

		Assert.assertNull("result - trans must be NULL, " , trans) ;
		
    }


	
	
	private InventoryBalanceBO getInventoryBalanceBO(String warehouseCode){
		InventoryBalanceBO b = (InventoryBalanceBO)appContext.getBean("inventoryBalanceBO") ;
		b.setWarehouseCode(warehouseCode) ;
		return b ;
		//return new InventoryBalanceBO(warehouseCode) ;
	}
}
